<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%@include file="../../common/include-header.jsp" %>

<body class="gray-bg">

<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="role-form">
                <div class="select-list">
                    <ul>
                        <li>
                            年级：
                            <select name="gradeId" id="gradeId" style="width: 170px" >
                                <option value="">请选择</option>
                                <c:forEach items="${grades}" var="grade">
                                    <option value="${grade.id}">${grade.name}</option>
                                </c:forEach>
                            </select>
                        </li>
                        <li>
                            课程：
                            <select name="courseId" id="courseId" style="width: 170px" >
                                <option value="">请选择</option>
                            </select>
                        </li>
                        <li>
                            章节：
                            <select name="chapterId" id="chapterId" style="width: 170px" disabled>
                                <option value="">请选择</option>
                            </select>
                        </li>
                        <li>
                            <%--试题类型：<input type="text" name="type" style="width: 170px"/>--%>
                            试题类型：
                            <select name="typeId">
                                <option value="">请选择</option>
                                <option value="10000">单选</option>
                               <option value="10001">多选</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()">
                <i class="fa fa-plus"></i> 新增
            </a>
            <%--<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()">
                <i class="fa fa-edit"></i> 修改
            </a>--%>
            <a class="btn btn-danger btn-del disabled" onclick="$.operate.removeAll()">
                <i class="fa fa-remove"></i> 批量删除
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<%@include file="../../common/include-footer.jsp" %>
<script th:inline="javascript">
    var prefix = "${pageContext.request.contextPath}/question";
    $(function () {
        $('select[name=gradeId]').change(function () {
            var gradeId = $(this).val();
            if(gradeId!=null){
                $.ajax({
                    url: "${pageContext.request.contextPath}/question/getCourseByGradeId",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    data: {"gradeId": gradeId},    //参数值
                    type: "POST",   //请求方式
                    success: function (data) {
                        $('.hz1').remove();
                        var courses = data;
                        for (var i = 0; i < courses.length; i++) {
                            var name = courses[i].name;
                            var id = courses[i].id;
                            //给下拉框动态赋值
                            $("select[name=courseId]").append("<option class='hz1' value='" + id + "'>" + name + "</option>");
                        }
                    }
                });
                $('select[name=chapterId]').attr("disabled", true);
            }
        });
        $('select[name=courseId]').change(function () {
            var courseId = $('select[name=courseId]').val();
            if(courseId!=null){
                $.ajax({
                    url: "${pageContext.request.contextPath}/question/getChapterByCourseId",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    data: {"courseId": courseId},    //参数值
                    type: "POST",   //请求方式
                    success: function (data) {
                        var chapters = data;
                        $('.hz2').remove();
                        for (var i = 0; i < chapters.length; i++) {
                            var name = chapters[i].name;
                            var id = chapters[i].id;
                            //给下拉框动态赋值
                            $("select[name=chapterId]").append("<option class='hz2' value='" + id + "'>" + name + "</option>");
                        }
                        // alert($("#subid").val());
                    }
                });
                $('select[name=chapterId]').attr("disabled", false);
            }
        });

        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/addQuestion",
            updateUrl: prefix + "/update/{id}",
            removeUrl: prefix + "/delete",
            sortName: "",
            // rememberSelected: true,
            height: 500,
            modalName: "试题",
            columns: [{
                field: 'state',
                checkbox: true
            },
                {
                    field: 'id',
                    title: '试题ID'
                },
                {
                    field: 'gradeName',
                    title: '年级',
                },
                {
                    field: 'courseName',
                    title: '课程',
                },
                {
                    field: 'chapterName',
                    title: '章节',
                },
                {
                    field: 'question',
                    title: '题干',
                },
                {
                    field: 'answer',
                    title: '答案',
                },
                {
                    field: 'typeId',
                    title: '试题类型',
                    formatter: function (value, item, index) {
                        if (item.typeId == '10000') {
                            return '<span class="label label-success">单选</span>';
                        } else if (item.typeId == '10001') {
                            return '<span class="label label-primary">多选</span>';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs ' + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

        //科目下拉款初始化
        $.ajax({
            url:"/onlineExam/course/getcourse",    //请求的url地址
            dataType:"json",   //返回格式为json
            async:true,//请求是否异步，默认为异步，这也是ajax重要特性
            data:{"id":"无"},    //参数值
            type:"POST",   //请求方式
            success:function(data){
                var list = data.list;
                for(var i=0;i<list.length;i++){
                    var subid = list[i].subid;
                    var subname =list[i].subname;
                    // $("#subid").val([subid,subname]);
                    //给下拉框动态赋值
                    $("#subid").append("<option value='"+subid+"'>"+subname+"</option>");
                }
                // alert($("#subid").val());
            }

        });

    });
</script>
</body>
</html>
